import { createRouter, createWebHistory } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 导入布局组件
import Layout from '@/views/layout/index.vue'

// 路由配置
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/',
    redirect: '/dashboard'
  },
  {
    path: '/dashboard',
    name: 'Dashboard',
    component: Layout,
    meta: { title: '首页' },
    children: [
      {
        path: '',
        component: () => import('@/views/dashboard/index.vue'),
        meta: { title: '首页' }
      }
    ]
  },
  // 基础信息
  {
    path: '/base-info',
    component: Layout,
    redirect: '/base-info/supplier',
    meta: { title: '基础信息' },
    children: [
      {
        path: 'supplier',
        name: 'SupplierManagement',
        component: () => import('@/views/base-info/supplier/index.vue'),
        meta: { title: '供应商管理' }
      },
      {
        path: 'customer',
        name: 'CustomerManagement',
        component: () => import('@/views/base-info/customer/index.vue'),
        meta: { title: '客户管理' }
      },
      {
        path: 'owner',
        name: 'OwnerManagement',
        component: () => import('@/views/base-info/owner/index.vue'),
        meta: { title: '货主管理' }
      },
      {
        path: 'product',
        name: 'ProductManagement',
        component: () => import('@/views/base-info/product/index.vue'),
        meta: { title: '商品管理' }
      },
      {
        path: 'warehouse',
        name: 'WarehouseManagement',
        component: () => import('@/views/base-info/warehouse/index.vue'),
        meta: { title: '仓库管理' }
      },
      {
        path: 'warehouse-area',
        name: 'AreaManagement',
        component: () => import('@/views/base-info/warehouse-area/index.vue'),
        meta: { title: '库区管理' }
      },
      {
        path: 'storage-shelf',
        name: 'ShelfManagement',
        component: () => import('@/views/base-info/storage-shelf/index.vue'),
        meta: { title: '货架管理' }
      },
      {
        path: 'storage-location',
        name: 'LocationManagement',
        component: () => import('@/views/base-info/storage-location/index.vue'),
        meta: { title: '货位管理' }
      }
    ]
  },
  // 商品
  {
    path: '/commodity',
    component: Layout,
    redirect: '/commodity/info',
    meta: { title: '商品' },
    children: [
      {
        path: 'info',
        name: 'commodityInfo',
        component: () => import('@/views/commodity/commodity-info/index.vue'),
        meta: { title: '商品信息' }
      },
      {
        path: 'property',
        name: 'commodityProperty',
        component: () => import('@/views/commodity/commodity-property/index.vue'),
        meta: { title: '商品属性' }
      }
    ]
  },
  // 用户权限
  {
    path: '/user-permission',
    component: Layout,
    redirect: '/user-permission/user',
    meta: { title: '用户权限' },
    children: [
      {
        path: 'user',
        name: 'UserManagement',
        component: () => import('@/views/user-permission/user/index.vue'),
        meta: { title: '用户管理' }
      },
      {
        path: 'role',
        name: 'RolePermissionManagement',
        component: () => import('@/views/user-permission/role/index.vue'),
        meta: { title: '角色权限管理' }
      }
    ]
  },
  
  // 入库模块
  {
    path: '/inbound',
    component: Layout,
    redirect: '/inbound/pending-operation',
    meta: { title: '入库模块' },
    children: [
      {
        path: 'pending-operation',
        name: 'PendingOperationManagement',
        component: () => import('@/views/inbound/pending-operation/index.vue'),
        meta: { title: '入库待作业管理' }
      },
      {
        path: 'receiving',
        name: 'ReceivingManagement',
        component: () => import('@/views/inbound/receiving/index.vue'),
        meta: { title: '收货入库管理' }
      },
      {
        path: 'putaway',
        name: 'PutawayManagement',
        component: () => import('@/views/inbound/putaway/index.vue'),
        meta: { title: '上架管理' }
      }
    ]
  },
  
  // 出库模块
  {
    path: '/outbound',
    component: Layout,
    redirect: '/outbound/pending-allocation',
    meta: { title: '出库模块' },
    children: [
      {
        path: 'pending-allocation',
        name: 'PendingAllocationManagement',
        component: () => import('@/views/outbound/pending-allocation/index.vue'),
        meta: { title: '出库待分配管理' }
      },
      {
        path: 'order-picking',
        name: 'OrderPickingManagement',
        component: () => import('@/views/outbound/order-picking/index.vue'),
        meta: { title: '按单配货管理' }
      },
      {
        path: 'check',
        name: 'CheckManagement',
        component: () => import('@/views/outbound/check/index.vue'),
        meta: { title: '复核验货管理' }
      },
      {
        path: 'shortage-order',
        name: 'ShortageOrderManagement',
        component: () => import('@/views/outbound/shortage-order/index.vue'),
        meta: { title: '缺货订单管理' }
      },
      {
        path: 'pending-outbound',
        name: 'PendingOutboundManagement',
        component: () => import('@/views/outbound/pending-outbound/index.vue'),
        meta: { title: '待出库管理' }
      },
      {
        path: 'order-query',
        name: 'OrderQueryManagement',
        component: () => import('@/views/outbound/order-query/index.vue'),
        meta: { title: '订单查询管理' }
      },
      {
        path: 'inventory-count',
        name: 'InventoryCountManagement',
        component: () => import('@/views/outbound/inventory-count/index.vue'),
        meta: { title: '盘点功能管理' }
      }
    ]
  },
  
  // 库内模块
  {
    path: '/inventory',
    component: Layout,
    redirect: '/inventory/discrepancy',
    meta: { title: '库内模块' },
    children: [
      {
        path: 'discrepancy',
        name: 'DiscrepancyManagement',
        component: () => import('@/views/inventory/discrepancy/index.vue'),
        meta: { title: '差异管理' }
      },
      {
        path: 'transfer',
        name: 'TransferManagement',
        component: () => import('@/views/inventory/transfer/index.vue'),
        meta: { title: '移库功能管理' }
      },
      {
        path: 'conversion',
        name: 'ConversionManagement',
        component: () => import('@/views/inventory/conversion/index.vue'),
        meta: { title: '正次转换管理' }
      },
      {
        path: 'inventory-query',
        name: 'InventoryQueryManagement',
        component: () => import('@/views/inventory/inventory-query/index.vue'),
        meta: { title: '库存查询管理' }
      },
      {
        path: 'location-query',
        name: 'LocationQueryManagement',
        component: () => import('@/views/inventory/location-query/index.vue'),
        meta: { title: '货位查询管理' }
      }
    ]
  },
  
  // 配置模块
  {
    path: '/configuration',
    component: Layout,
    redirect: '/configuration/inbound-config',
    meta: { title: '配置模块' },
    children: [
      {
        path: 'inbound-config',
        name: 'InboundConfigManagement',
        component: () => import('@/views/configuration/inbound-config/index.vue'),
        meta: { title: '入库配置管理' }
      },
      {
        path: 'outbound-config',
        name: 'OutboundConfigManagement',
        component: () => import('@/views/configuration/outbound-config/index.vue'),
        meta: { title: '出库配置管理' }
      },
      {
        path: 'warehouse-config',
        name: 'WarehouseConfigManagement',
        component: () => import('@/views/configuration/warehouse-config/index.vue'),
        meta: { title: '仓内配置管理' }
      }
    ]
  },
  // 404页面
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/error/404.vue'),
    meta: { title: '404' }
  },
  // 重定向页面
  {
    path: '/redirect/:path*',
    component: () => import('@/views/redirect/index.vue'),
    hidden: true
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  NProgress.start()
  document.title = to.meta.title ? `${to.meta.title} - elmentui-plus-admin` : 'elmentui-plus-admin'
  
  const token = localStorage.getItem('token')
  
  if (to.path === '/login') {
    next()
  } else {
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})

export default router 